/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.qdsg.ysg.doctor.ui.dialog;

import android.app.Dialog;
import android.location.Address;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;
import com.qdsg.ysg.doctor.util.SoftKeyboardUtil;
import com.qdsg.ysg.doctor.util.UtilString;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class ChooseAddressDialog extends BaseDialogFragment {
    private OnItemClickListener mListener;
    private Dialog mDialog;
    private AddMedicineAdapter adapter;
    private List<String> mList = new ArrayList<String>();
    private Address address;

    public static ChooseAddressDialog newInstance(Address address) {
        ChooseAddressDialog itemDialog = new ChooseAddressDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelable( "address", address );
        itemDialog.setArguments( bundle );
        return itemDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( true );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.CENTER );
        }
        return mDialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mView = inflater.inflate( R.layout.dialog_item_extra, container, false );
        mView.setMinimumWidth( mScreenWidth );
        assert getArguments() != null;
        address = (Address) getArguments().getParcelable( "address" );
        LinearLayoutManager manager = new LinearLayoutManager( getActivity() );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        RecyclerView recyclerView = mView.findViewById( R.id.recyclerView );
        ImageView iv_cancel = mView.findViewById( R.id.iv_cancel );
        TextView tvTitle = mView.findViewById( R.id.tv_title );
        EditText edt_search = mView.findViewById( R.id.edt_search );
        recyclerView.setLayoutManager( manager );
        adapter = new AddMedicineAdapter();
        recyclerView.setAdapter( adapter );

        edt_search.setVisibility( View.INVISIBLE );
        iv_cancel.setOnClickListener( v -> {
            if (mListener != null) {
                dismiss();
            }
        } );


        edt_search.setOnEditorActionListener( (v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // 按下完成按钮，这里和上面imeOptions对应
                SoftKeyboardUtil.hideSoftKeyboard( Objects.requireNonNull( getActivity() ) );
            }
            return false;
        } );
        edt_search.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        } );

        tvTitle.setText( "周边信息" );
        addressItemList();
        return mView;
    }

    @Override
    public void onViewCreated(@NotNull View view, Bundle savedInstanceState) {
        super.onViewCreated( view, savedInstanceState );
        Objects.requireNonNull( getDialog() ).setCanceledOnTouchOutside( false );
    }

    public interface OnItemClickListener {
        void onItem(String address);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }


    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    class AddMedicineAdapter extends RecyclerView.Adapter<AddMedicineAdapter.Holder> {


        @NotNull
        @Override
        public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder( LayoutInflater.from( getActivity() ).inflate( R.layout.item_textview, parent, false ) );
            return myHolder;
        }

        @Override
        public void onBindViewHolder(AddMedicineAdapter.Holder holder, final int position) {

            holder.itemView.setOnClickListener( v -> {
                mListener.onItem( mList.get( position ) );
                dismiss();
            } );
            holder.tv_name.setText( mList.get( position ) );
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name;

            Holder(View itemView) {
                super( itemView );
                tv_name = itemView.findViewById( R.id.text );
            }
        }
    }

    private void addressItemList() {
        mList.clear();
        for (int i = 0; address.getAddressLine( i ) != null; i++) {
            String addressLine = address.getAddressLine( i );
            //街道名称:广东省深圳市罗湖区蔡屋围一街深圳瑞吉酒店
            System.out.println( "addressLine=====" + addressLine );
            mList.add( addressLine );
        }

        adapter.notifyDataSetChanged();
    }

}
